let socket = require("socket.io");
let http = require("http");




// http
console.log();
let server = http.createServer();
let roomid = "1001";
let io = socket(server, {
  cors: {
    origin: "*",
  },
});
let userId = {};
io.on("connection", (socket) => {
  console.log("链接成功。。。");
  socket.emit("connectionSuccess", roomid);
  socket.join(roomid);
  /* 监听加入房间 */

  socket.on("joinRoom", (param) => {
    if (param && param.id && param.user) {
      // userId.push(param.id);
      userId[param.user] = param.id;
      socket.join(param.id);
      io.to(roomid).emit("getRoom", JSON.stringify(userId));
    }
  });


  // 发起通话
  socket.on("callRemote", (param) => {
    // userId
    if (param.peerid) {
      io.to(param.peerid).emit("acceptCall");
    }
  });
  // 接受通话
  socket.on("acceptCall", (roomid) => {
    io.to(roomid).emit("acceptCall");
  });

  // 监听上报id
  //
  // sock.on("reportsId", (param) => {
  //   if (param && param.id) {
  //     userId.push(param.id);
  //   }
  // });
});
// http://127.0.0.1:3001
server.listen(3001, () => {
  console.log("服务器启动成功 ");
});

